package com.pro.controller;

import com.pro.pojo.JsonData;
import com.pro.utils.FastDFSClientUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: UploadController
 * @Author: 笑笑
 * @DATE: 2022/1/11
 * @TIME: 下午 04:44
 */
@RestController
@RequestMapping("/Upload")
public class UploadController {

    @Autowired
    private FastDFSClientUtil fds;

    @RequestMapping(value = "/uploadImages")
    public JsonData uploadImg(MultipartFile[] files, HttpServletRequest request, HttpServletResponse response){
        List<String> list = new ArrayList<>();
        for (MultipartFile file : files) {
            try {
                //数据库文件路径
                String fileUrl = fds.uploadFile(file);
                //前台传递路径
                request.setAttribute("msg", "上传成功，"+fileUrl+"");
                list.add(fds.getResAccessUrl(fileUrl));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list.size() > 0 ? JsonData.buildSuccess(list) : JsonData.buildError("上传失败");
    }
}
